package com.youkeda.app.service.impl;

import com.youkeda.app.dao.ProductDao;
import com.youkeda.app.dataobject.ProductDO;
import com.youkeda.app.model.Result;
import com.youkeda.app.service.SnappedUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @ClassName SnappedUpServiceImpl
 * @Author 刘正星
 * @Date 2020/8/24 13:43
 **/
@Service
public class SnappedUpServiceImpl implements SnappedUpService {

    @Autowired
    private ProductDao productDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Result<Boolean> snappedUp(Long id) {
        Result result = new Result();
        result.setData(true);
        result.setSuccess(true);
        if (StringUtils.isEmpty(id)){
            result.setSuccess(false);
            result.setData(false);
            return result;
        }
        ProductDO productDO = productDao.selectByPrimaryKey(id);
        if (productDO.getStock() < 1){
            result.setData(false);
            return result;
        }
        productDO.setStock(productDO.getStock()-1);
       if (productDao.updateByPrimaryKeySelective(productDO) <= 0){
           result.setData(false);
           return result;
       }

        return result;
    }
}
